package com.drw.start.spring.session.controller;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SessionController {

    @RequestMapping("session")
    public String sessionPage(ServletRequest r, HttpSession session, Model m)
            throws UnknownHostException, SocketException {
        // InetAddress addr = InetAddress.getLocalHost();


        // Ubuntu虚拟机的网卡名称，不同的机器名称不同
        InetAddress addr = null;
        NetworkInterface i = NetworkInterface.getByName("enp0s3");
        if (i != null) {
            for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();) {
                addr = (InetAddress) en2.nextElement();
                if (!addr.isLoopbackAddress()) {
                    if (addr instanceof Inet4Address) {
                        System.out.println(addr.getHostAddress());
                        break;
                    }
                }
            }
            m.addAttribute("address", addr.getHostAddress());
        }
        return "session";
    }
}
